package com.example.demo.controller;

import com.example.demo.entity.attention;
import com.example.demo.entity.blog;
import com.example.demo.entity.blogdata;
import com.example.demo.entity.users;
import com.example.demo.mapper.attentionMapper;
import com.example.demo.mapper.blogMapper;
import com.example.demo.mapper.usersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
public class attentionController {
    @Autowired
    attentionMapper attentionMapper;
    @Autowired
    blogMapper blogMapper;
    @Autowired
    usersMapper usersMapper;

    //添加关注
    @GetMapping("/insertattention/{to}/{from}")
    public void insertattention(@PathVariable String to, @PathVariable String from) {
        attention a = new attention();
        a.setF(from);
        a.setT(to);
        attentionMapper.insertattention(a);
    }

    //取消关注
    @GetMapping("/deleteattention/{to}/{from}")
    public void deleteattention(@PathVariable String to, @PathVariable String from) {
        attention a = new attention();
        a.setT(to);
        a.setF(from);
        List<attention> list = new ArrayList<>();
        list.add(a);
        attentionMapper.deleteattention(list);
    }

    //关注查询
    @GetMapping("/selectattention/{to}/{from}")
    public String selectattention(@PathVariable String to, @PathVariable String from) {
        attention a = new attention();
        a.setT(to);
        a.setF(from);
        if (attentionMapper.selectattention(a).size() > 0) {
            return "ok";
        } else {
            return "no";
        }
    }

    //查询我关注的人
    @GetMapping("/selectAll/{from}")
    public String selectAll(@PathVariable String from) {
        List<blogdata> ds = new ArrayList<>();
        attention a = new attention();
        a.setF(from);
        //查询我所关注的人
        List<attention> la = attentionMapper.selectgz(a);
        for (int t = 0; t < la.size(); t++) {
            List<blog> blogs = blogMapper.selectblogBymailbox(la.get(t).getT());
            blogdata blogdata = new blogdata();
            for (int i = 0; i < blogs.size(); i++) {
                blogdata.setBrowse(blogs.get(i).getBrowse() + blogdata.getBrowse());
                blogdata.setComment(blogs.get(i).getComment() + blogdata.getComment());
                blogdata.setFabulous(blogs.get(i).getFabulous() + blogdata.getFabulous());
                blogdata.setCollection(blogs.get(i).getCollection() + blogdata.getCollection());
            }
            attention as = new attention();
            as.setT(la.get(t).getT());
            blogdata.setFans(attentionMapper.selectFans(as).size());
            blogdata.setQriginal(blogs.size());
            blogdata.setGrade(usersMapper.selectuser(as.getT()).getGrade());

            DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            users u = usersMapper.selectuser(as.getT());
            try {
                Date d2 = df.parse(u.getFirstlinecode());
                Date d1 = new Date();
                long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
                long days = diff / (1000 * 60 * 60 * 24);
                blogdata.setCodeage(Double.parseDouble(String.format("%.1f", days * 1.0 / 365.0)));
                blogdata.setUsers(u);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            ds.add(blogdata);
        }
        return ds.toString();
    }
}
